package com.rise.serialportdemo;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.rise.serialportdemo.cmd4000.BaseAppCompatActivity;
import com.rise.serialportdemo.cmd4000.Device;
import com.rise.serialportdemo.cmd4000.SerialPortManager;
import com.rise.serialportdemo.cmd4000.utils.ByteUtil;
import com.rise.serialportdemo.cmd4000.utils.Logs;
import com.rise.serialportdemo.cmd4000.utils.XORUtil;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 出钞-CMD4000的测试类
 */
public class CMD4000Activity extends BaseAppCompatActivity {

    @BindView(R.id.bt_OpenCDM)
    Button bt_OpenCDM;

    @BindView(R.id.bt_CloseCDM)
    Button bt_CloseCDM;

    @BindView(R.id.bt_initializeCDM)
    Button mBtnInitializeCDM;

    @BindView(R.id.bt_Diagnostics)
    Button mBtnDiagnostics;

    @BindView(R.id.bt_Banknote)
    Button mBtBanknote;

    @BindView(R.id.tv_test)
    TextView tv_test;

    private Device mDevice;

    private SerialControl serialControl;

    private boolean mOpened = false;

    // 串口路径
    private String mDevices = "/dev/ttyS3";
    //波特率
    private String mBaudrates = "9600";

    //初始化CDM
    private static final String ATM_INITIALIZE_COM = "0201300332";
    //诊断
    private static final String ATM_DIAGNOSTICS = "0201320330";
    //Multi Cassette Dispense多个钱箱出币
    private static final String ATM_MULTI_CASSETTE_DISPENSE = "02073A010000000000033F";
    //05
    private static final String ATM_TX_05 = "05";
    //06
    private static final String ATM_TX_06 = "06";

    // 0 - 初始化 1
    private boolean isInitializeCDM = false;

    private StringBuilder xorSB;
    private StringBuilder stringBuilder;

    StringBuilder sMsg = new StringBuilder();

    @Override
    protected void initView() {
        serialControl = new SerialControl();
        mDevice = new Device(mDevices, mBaudrates);

        openSerialPort();
        sendData(ATM_DIAGNOSTICS);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_cmd_4000;
    }

    @OnClick({R.id.bt_OpenCDM, R.id.bt_CloseCDM, R.id.bt_initializeCDM, R.id.bt_Diagnostics, R.id.bt_Banknote})
    public void onClickView(View view) {
        switch (view.getId()) {

            case R.id.bt_OpenCDM:
                openSerialPort();
                break;

            case R.id.bt_CloseCDM:
                closeSerialPort();
                break;

            case R.id.bt_initializeCDM:
                sendData(ATM_INITIALIZE_COM);
                break;

            case R.id.bt_Diagnostics:
                sendData(ATM_DIAGNOSTICS);
                break;

            case R.id.bt_Banknote://出钞票
                sMsg.setLength(0);
                //10进制转16进制
                String hex = ByteUtil.decimal2fitHex(3);
                //02073A05000000000003
                xorSB = new StringBuilder().append("073A").append(hex).append("000000000003");
                //异或算法
                String xor = XORUtil.checkcode_0007(xorSB.toString());
                stringBuilder = new StringBuilder().append("02073A").append(hex).append("000000000003").append(xor);

                sendData(stringBuilder.toString());
                break;
        }
    }

    private String COM_DATE;

    //发送数据
    private void sendData(String text) {

        if (ATM_DIAGNOSTICS.equals(text)) {
            COM_DATE = ATM_DIAGNOSTICS;
        } else if (stringBuilder != null && !TextUtils.isEmpty(stringBuilder.toString()) && stringBuilder.toString().equals(text)) {
            COM_DATE = stringBuilder.toString();
        } else if (ATM_TX_05.equals(text)) {
            COM_DATE = ATM_TX_05;
        } else if (ATM_TX_06.equals(text)) {
            COM_DATE = ATM_TX_06;
        }

        //String text = mEtData.getText().toString().trim();
        if (TextUtils.isEmpty(text) || text.length() % 2 != 0) {
            showToast("无效数据");
            return;
        }
        //SerialPortManager.instance().sendCommand(text);
        serialControl.sendCommand(text);
    }

    //打开串口
    private void openSerialPort() {
        mOpened = serialControl.open(mDevice, 1, 1, 'N') != null;
        if (mOpened) {
            showToast("成功打开串口");
        } else {
            showToast("打开串口失败");
        }
    }

    //关闭串口
    private void closeSerialPort() {
        if (serialControl != null) {
            serialControl.close();
        }
    }

    //串口控制
    private class SerialControl extends SerialPortManager {

        private SerialControl() {

        }

        @Override
        protected void onDataReceived(byte[] received, int size) {
            final String hexStr = ByteUtil.bytes2HexStr(received, 0, size);
            sMsg.append(hexStr);
            Logs.v("接收的数据===》" + sMsg.toString());
            if (!TextUtils.isEmpty(sMsg.toString())) {
                reset(sMsg.toString());
            }
        }
    }

    private String COM_DATA_05;

    private void reset(String hexStr) {

        //诊断
        if (!TextUtils.isEmpty(COM_DATE) && COM_DATE.equals(ATM_DIAGNOSTICS) && !TextUtils.isEmpty(hexStr) && hexStr.equals("06")) {
            COM_DATA_05 = "Diagnosis";
            Logs.v("正在诊断        ===》" + hexStr);
            sendData(ATM_TX_05);
        }

        //出钞
        if (!TextUtils.isEmpty(COM_DATE) && stringBuilder != null && !TextUtils.isEmpty(stringBuilder.toString()) && COM_DATE.equals(stringBuilder.toString()) && !TextUtils.isEmpty(hexStr) && hexStr.equals("06")) {
            COM_DATA_05 = "Banknote";
            Logs.v("正在出钞        ===》" + hexStr);
            sendData(ATM_TX_05);
        }

        //诊断
        if (!TextUtils.isEmpty(COM_DATA_05) && COM_DATA_05.equals("Diagnosis") && !TextUtils.isEmpty(hexStr)) {
            if ( hexStr.contains("30")){
                sendData(ATM_TX_06);
                Logs.v("诊断成功 success        ===》" + hexStr);
                finish();
            }else {
                if (hexStr.length() > 8){
                    Logs.v("诊断失败 fail           ===》" + hexStr);
                }
            }
        }

        //出钞
        if (!TextUtils.isEmpty(COM_DATA_05) && COM_DATA_05.equals("Banknote") && !TextUtils.isEmpty(hexStr)) {
            if (hexStr.contains("30")) {
                sendData(ATM_TX_06);
                Logs.v("出钞成功 success        ===》" + hexStr);
                finish();
            } else {
                if (hexStr.length() > 8) {
                    Logs.v("出钞失败 fail           ===》" + hexStr);
                }
            }
        }
    }
}

